package com.pth.dao.impl;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.pth.dao.UserInfoDAO;
import com.pth.model.UserInfo;
import com.pth.utils.HopeState;
import com.pth.utils.KaoShengState;

public class UserInfoDAOImpl implements UserInfoDAO {

	private HibernateTemplate hibernateTemplate;
	
	public void save(UserInfo userInfo) {

		hibernateTemplate.save(userInfo);
		
	}

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public UserInfo getInfoByuid(int uid) {

		return hibernateTemplate.get(UserInfo.class, uid);
		
	}

	public void update(UserInfo userInfo) {

		hibernateTemplate.update(userInfo);
		
		
	}

	@SuppressWarnings("unchecked")
	public List<UserInfo> getallUser(final int offset,final int pagesize) {

		final String hql="from UserInfo u order by u.exam.examtime desc";
		List list=hibernateTemplate.executeFind(new HibernateCallback() {

			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {

				return arg0.createQuery(hql).setFirstResult(offset)
					.setMaxResults(pagesize).list();
			}
		});
		
		return list;
		
	}

	public void delete(UserInfo userInfo) {

		hibernateTemplate.delete(userInfo);
		
	}

	public int getallNum() {

		String hql="from UserInfo";
		
		return hibernateTemplate.find(hql).size();
		
	}

	@SuppressWarnings("unchecked")
	public UserInfo getUbyidentitycard(String identitycard) {

		String hql="from UserInfo u where u.identitycard='"+identitycard+"'";
		List<UserInfo> userInfos=hibernateTemplate.find(hql);
		
		
		if(userInfos.size()==0){
			
			return null;
			
		}
		
		Iterator<UserInfo> iterator=userInfos.iterator();
		
		UserInfo u=new UserInfo();
		
		while(iterator.hasNext()){
			
			u=iterator.next();
			
		}
		return u;
		
	}

	public boolean login(UserInfo userInfo) {

		boolean flag=false;
		String hql="from UserInfo u where u.name='"+userInfo.getName()+"' and u.identitycard='"+userInfo.getIdentitycard() +"'";
		int num=hibernateTemplate.find(hql).size();
		
		if(num!=0){
			flag=true;
			
		}
		return flag;
		
	}

	@SuppressWarnings("unchecked")
	public List<UserInfo> getUserByeid(final int offset,final int pagesize, int eid) {

		final String hql="from UserInfo u where u.exam.eid="+eid+" order by u.exam.examtime desc ";
		List list=hibernateTemplate.executeFind(new HibernateCallback() {

			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {

				return arg0.createQuery(hql).setFirstResult(offset)
					.setMaxResults(pagesize).list();
			}
		});
		
		return list;
	}

	public int getNumByeid(int eid) {
		 
		String hql="from UserInfo u where u.exam.eid="+eid;
		
		return hibernateTemplate.find(hql).size();
		
	}

	public UserInfo getUserInfoByCard(String card) {

		
		String hql="from UserInfo u where u.identitycard=:cardstring";
		
		String paramName= "cardstring"; 

	    String value= card; 

	    return    (UserInfo) this.getHibernateTemplate().findByNamedParam(hql, paramName, value); 
		
	}

	@SuppressWarnings("unchecked")
	public List<UserInfo> getUserInfosByState(int state,final int offset,
		final	int pagesize) {


		final String hql="from UserInfo u where u.state="+state+" order by u.exam.examtime desc";
		List list=hibernateTemplate.executeFind(new HibernateCallback() {

			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {

				return arg0.createQuery(hql).setFirstResult(offset)
					.setMaxResults(pagesize).list();
			}
		});
		
		return list;
		
		
	}

	@SuppressWarnings("unchecked")
	public List<UserInfo> getUserInfosByStateAndEid(int state,final int offset,
		final	int pagesize, int eid) {


		final String hql="from UserInfo u where u.state="+state+" and u.exam.eid="+eid+" order by u.exam.examtime desc";
		List list=hibernateTemplate.executeFind(new HibernateCallback() {

			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {

				return arg0.createQuery(hql).setFirstResult(offset)
					.setMaxResults(pagesize).list();
			}
		});
		
		return list;
		
		
	}

	public int getNumBystate(int state) {

		String hql="from UserInfo u where u.state="+state+" ";
		
		return hibernateTemplate.find(hql).size();
	}

	public int getNumBystateAndEid(int state, int eid) {

		String hql="from UserInfo u where u.state="+state+" and u.exam.eid="+eid+" ";
	
		return hibernateTemplate.find(hql).size();
	}

	@SuppressWarnings("unchecked")
	public List<UserInfo> getUserByeidNOPager(int eid) {

		String hqlString="from UserInfo u where u.exam.eid="+eid+"";
		
		return hibernateTemplate.find(hqlString);
		
		
	}

	@SuppressWarnings("unchecked")
	public List<UserInfo> getUserinfoByeid(int eid) {

		String hql="from UserInfo u where u.exam.eid="+eid;
		return hibernateTemplate.find(hql);
		
	}

	@SuppressWarnings("unchecked")
	public List<UserInfo> getInfosByCardandEid(String card, int eid) {

		String hqlString="from UserInfo u where u.exam.eid="+eid+" and u.identitycard='"+card+"' ";
		
		return hibernateTemplate.find(hqlString);
		
	}

	public int getNumbyHope(String hope,int state,int eid) {

		String hql=null;
		
		if(state==HopeState.HOPE_PEIXUN){
			
			hql="from UserInfo u where u.hopepx='"+hope+"' and eid="+eid;
		}
		else if(state==HopeState.HOPE_LINGZHENG){
			
			hql="from UserInfo u where u.hopelz='"+hope+"' and eid="+eid;
		}
		
		return hibernateTemplate.find(hql).size();
		
		
	}

	@SuppressWarnings("unchecked")
	public List<UserInfo> getInofoByCard_State_yes(String card, int state) {

		String hqlString="from UserInfo u where u.exam.state="+state+" and u.identitycard='"+card+"' ";
		
		return hibernateTemplate.find(hqlString);
		
	}

}
